package cn.com.zal.cgweb.pojo;

import java.util.Date;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * TcUser entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.com.zal.cgweb.pojo.TcUser
 * @author MyEclipse Persistence Tools
 */

public class TcUserDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(TcUserDAO.class);
	// property constants
	public static final String USERNAME = "username";
	public static final String TRUENAME = "truename";
	public static final String PASSWORD = "password";
	public static final String COMPANY_ID = "companyId";
	public static final String PHONE = "phone";
	public static final String MOBILE_PHONE = "mobilePhone";
	public static final String FAX = "fax";
	public static final String EMAIL = "email";
	public static final String ADDRESS = "address";
	public static final String STATE = "state";

	public void save(TcUser transientInstance) {
		log.debug("saving TcUser instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TcUser persistentInstance) {
		log.debug("deleting TcUser instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TcUser findById(java.lang.Long id) {
		log.debug("getting TcUser instance with id: " + id);
		try {
			TcUser instance = (TcUser) getSession().get(
					"cn.com.zal.cgweb.pojo.TcUser", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(TcUser instance) {
		log.debug("finding TcUser instance by example");
		try {
			List results = getSession().createCriteria(
					"cn.com.zal.cgweb.pojo.TcUser").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TcUser instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TcUser as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findByTruename(Object truename) {
		return findByProperty(TRUENAME, truename);
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findByCompanyId(Object companyId) {
		return findByProperty(COMPANY_ID, companyId);
	}

	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List findByMobilePhone(Object mobilePhone) {
		return findByProperty(MOBILE_PHONE, mobilePhone);
	}

	public List findByFax(Object fax) {
		return findByProperty(FAX, fax);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findAll() {
		log.debug("finding all TcUser instances");
		try {
			String queryString = "from TcUser";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TcUser merge(TcUser detachedInstance) {
		log.debug("merging TcUser instance");
		try {
			TcUser result = (TcUser) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TcUser instance) {
		log.debug("attaching dirty TcUser instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TcUser instance) {
		log.debug("attaching clean TcUser instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}